import api from '~api/index'
import util from '~src/lib/util'
export default {
  state: {
    c_list: {
      data: [],
      page: 1,
      id:'-1',
      code:'0',
      total:0
    },
    v_list: {
      data: [],
      page: 1,
      code:'0',
      id:'-1',
      total:0
    }
  },
  mutations: {
    ['setvipCouponList'] (state, data) {
      if (data.code == '200') {
        if (data.page === 1) {
          data.data = [].concat(data.data)
          if (data.data.length === 0) {
            data.data = []
          }
        } else {
          data.data = state.v_list.data.concat(data.data)
        }
        state.v_list = data
        state.v_list.total = data.total
      }
      state.v_list.code = data.code
    },
    ['setCouponList'] (state, data) {
      if (data.code == '200') {
        if (data.page === 1) {
          data.data = [].concat(data.data)
          if (data.data.length === 0){
            data.data = []
          }
        } else {
          data.data = state.c_list.data.concat(data.data)
        }
        state.c_list = data
        state.c_list.total = data.total
      }
      state.c_list.code = data.code
    },
    ['appendCouponList'] (state, data) {
      var total = parseInt(state.c_list.total)
      state.c_list.data.splice(0,0,data)
      total += 1
      state.c_list.total = total
    }
  },
  actions: {
    async ['getvipCouponList'] ({commit}, config) {
      const { data: { data, code } } = await api.post('/v1/memberCouponList',
        {openid: util.localItem.get('openid'), card_id: config.id, page: config.page, page_size: 10})
      if (data && code === '200') {
        commit('setCouponList', {
          data: data.coupon_list.length>0?data.coupon_list:[],
          page: config.page,
          total:data.total,
          code: code
        })
        config.callback && config.callback()
      }else{
        console.log(code)
        commit(setCouponList, {
          data: [],
          page: config.page,
          code: code,
          total:data.total,
          code: code
        })
        config.stopCallback && config.stopCallback()
      }
    },
    async ['getCouponList'] ({commit}, config) {
      const { data: { data, code } } = await api.post('/v1/getCouponList',
        {openid: util.localItem.get('openid'), page: config.page, page_size: 10})
      if (data && code === '200') {
        commit('setvipCouponList', {
          data: data.coupon_list.length>0?data.coupon_list:[],
          page: config.page,
          total:data.total,
          code: code
        })
        config.callback && config.callback()
      }else{
        commit(setvipCouponList, {
          data: [],
          page: config.page,
          code: code
        })
        config.stopCallback && config.stopCallback()
      }
    },
    async ['AddCoupon'] ({commit}, config) {
      if (config.code === '') {
        commit('TOAST_SHOW', {content: '优惠劵不能为空', msg: true})
        return
      }
      const { data: { data, code ,msg } } = await api.post('/v1/takeCoupon',
        {openid: util.localItem.get('openid'), card_id: config.id, coupon_code: config.code})
      if (data && code === '200') {
        commit('TOAST_SHOW', {content: '添加成功', msg: true})
        commit('appendCouponList', data.quan_info)
        config.callback && config.callback()
      } else {
        commit('TOAST_SHOW', {content: msg, msg: true})
      }
    }
  },
  getters: {
    ['getCouponList'] (state) {
      return state.v_list
    },
    ['getvipCouponList'] (state) {
      return state.c_list
    }
  }
}
